<template>
	<view>
		<view class="list-call">
			<image class="img" src="/static/shilu-login/4.png"></image>
			<input class="biaoti" v-model="name" maxlength="11" placeholder="姓名" />
		</view>
		<view class="list-call">
			<image class="img" src="/static/shilu-login/1.png"></image>
			<input class="biaoti" v-model="phone" type="number" maxlength="11" placeholder="手机号" />
			<!-- <button class="cu-btn bg-green" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取本机号码</button> -->
		</view>
		<view class="btnContainer"><button class="cu-btn round bg-yellow" @click="regClick">注册</button></view>
	</view>
</template>

<script>
import { mapActions } from 'vuex';
import subMessageIds from '@/config/subMessageIds.js';
export default {
	data() {
		return {
			name: '',
			phone: ''
		};
	},
	methods: {
		...mapActions({
			agentRegister: 'agent/agentRegister'
		}),
		regClick() {
			let self = this
			wx.requestSubscribeMessage({
				tmplIds: [subMessageIds.receiveRegMsg],
				success(res) {
					console.log(res);
				},
				fail(err) {
					console.log(err);
				},
				complete() {
					if (!/^1[3456789]\d{9}$/.test(self.phone)) {
						uni.showToast({
							icon: 'none',
							title: '请输入正确的手机号'
						});
						return;
					}
					if (self.name == '') {
						uni.showToast({
							icon: 'none',
							title: '请输入姓名'
						});
						return;
					}
					uni.showLoading({
						title: '请求中',
						mask: true
					});
					self.agentRegister({
						tel: self.phone,
						name: self.name,
						wxAppOpenId: self.$store.state.user.userInfo.openId
					})
						.then(res => {
							uni.hideLoading();
							console.log(res);
							if (res) {
								switch (res.code) {
									case '000000': {
										uni.showModal({
											content: res.msg,
											showCancel: false,
											success: function(res) {
												if (res.confirm) {
													console.log('用户点击确定');
													uni.navigateBack({
														delta: 1
													});
												} else if (res.cancel) {
													console.log('用户点击取消');
												}
											}
										});
										break;
									}
									default: {
										self.$nextTick(function() {
											uni.showModal({
												content: res.msg,
												showCancel: false,
												success: function(res) {
													if (res.confirm) {
														console.log('用户点击确定');
													} else if (res.cancel) {
														console.log('用户点击取消');
													}
												}
											});
										});

										break;
									}
								}
							}
						})
						.catch(err => {
							console.log(err);
							uni.hideLoading();
						});
				}
			});
		}
	}
};
</script>

<style></style>
